package com.example.xxpk.controller.purchaserecord;

import com.example.xxpk.common.result.R;
import com.example.xxpk.pojo.PurchaseRecord;
import com.example.xxpk.service.boxes.IBoxesService;
import com.example.xxpk.service.outifts.IOutiftsService;
import com.example.xxpk.service.purchaserecord.IPurchaseRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangguihong
 * @since 2025-02-19
 */
@RestController
@RequestMapping("/purchaseRecord")
@RequiredArgsConstructor
@Api(tags="购买表")
public class PurchaseRecordController {

    private final IPurchaseRecordService purchaseRecordService;
    private final IOutiftsService outiftsService;
    private final IBoxesService boxesService;

    @PostMapping("/add")
    @ApiOperation("新增购买记录")
    public R addPurchaseRecord(@RequestBody PurchaseRecord purchaseRecord) {
        Integer type = purchaseRecord.getType();
        boolean b = true;
        if (type == 1){ //购买装扮
             b = outiftsService.purchaseOutfit(purchaseRecord.getUserId(), purchaseRecord.getTypeId());
        }
        if (type == 2){ //购买普通宝箱
             b = boxesService.addPurchaseBoxes(purchaseRecord);
        }
        if (type == 3){ //购买超级宝箱
             b = boxesService.addPurchaseSuperBoxes(purchaseRecord);
        }
        boolean result = purchaseRecordService.addPurchaseRecord(purchaseRecord);
        if (result && b) {
            return R.ok("购买成功");
        } else {
            return R.error("购买失败");
        }
    }


}
